/*
 * @lc app=leetcode.cn id=682 lang=javascript
 *
 * [682] 棒球比赛
 */

// @lc code=start
/**
 * @param {string[]} ops
 * @return {number}
 */
var calPoints = function(ops) {
    let res=[]
    ops.forEach((item)=>{
       if( isFinite(+item)){
           res.push(item)
       }else{
           switch(item){
               case "C":res.pop();break;
               case "D":res.push((+res[res.length-1])*2);break;
               case("+"):{res.push(+res[res.length-1]+(+res[res.length-2]));break}
           }
       }
    })
    let sum=res.reduce((a,b)=>parseInt(a)+parseInt( b))
    return sum
};
// @lc code=end

calPoints(["5","2","C","D","+"])